package com.qst.survey.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @Date: 2021/3/20 16:27
 * @Description:
 */
@Component
public class RedisUtils {

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 存入验证码到redis
     *
     * @param email
     * @param code
     */
    public void setCode(String email, String code) {
        redisTemplate.opsForValue().set(email, code, 10, TimeUnit.MINUTES);
    }

    /**
     * 获取redis的验证码
     *
     * @param email
     * @return
     */
    public String getCode(String email) {
        return (String) redisTemplate.opsForValue().get(email);

    }


    /**
     * 根据key删除redis中的数据
     */
    public void delete(String key) {
        redisTemplate.delete(key);
    }

    /**
     * 存储token到redis
     *
     * @param username
     * @param Token
     */
    public void setToken(String username, String Token) {
        redisTemplate.opsForValue().set(username, Token, 30, TimeUnit.MINUTES);
    }

    /**
     * 获取Redis中的Token
     *
     * @param
     * @return
     */
    public String getToken(String username) {
        return (String) redisTemplate.opsForValue().get(username);
    }

    public void addTime(String key){
        redisTemplate.expire(key,30, TimeUnit.MINUTES);
    }



}
